/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.itttq.gshop.domain.po.UserFavoritesPo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.pbpm.service.UserFavoritesService;

@RestController
@RequestMapping("favorite")
public class UserFavoritesController extends GenericController {
	private static final long serialVersionUID = -8946967546474052L;
	@Autowired
	private UserFavoritesService userFavoritesService;
	
	/**
	 * 根据条件获取UserFavoritesPo对象，并返回查询的数据总数
	 * 
	 * @param userId 用户ID
	 * @Author 迷蝶
	 * @return ResultPageTo<UserFavoritesPo>
	 */
	@RequestMapping("list/{userId}")
	public ResultPageTo<UserFavoritesPo> getUserFavoritess(@PathVariable("userId")int userId) {
		UserFavoritesPo favorites = new UserFavoritesPo();
		favorites.setUserId(userId);
		return userFavoritesService.getUserFavoritess(favorites);
	}
	
	/**
	 * 判断用户是否收藏此商品
	 * 
	 * @param userId 用户ID
	 * @param wareId 商品ID
	 * @Author 迷蝶
	 * @return Boolean
	 */
	@RequestMapping("isFavorite/{wareId:^[0-9]*$}/{userId:^[0-9]*$}")
    public Integer isFavorite(@PathVariable("wareId") int wareId, @PathVariable("userId") int userId) {
		UserFavoritesPo fav = new UserFavoritesPo();
		fav.setUserId(userId);
		fav.setWareId(wareId);
        return userFavoritesService.isFavoritess(fav);
	}
	/**
	 * 根据ID获取UserFavoritesPo对象，并放入redis缓存
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return UserFavoritesPo
	 */
	@RequestMapping("{id:^[0-9]*$}")
	public UserFavoritesPo getUserFavorites(@PathVariable("id") int id) {
		return userFavoritesService.getUserFavorites(id);
	}
	
	/**
	 * 保存UserFavoritesPo对象，并清除此ID的缓存。<br>
	 * 如果ID存在，则更新对象；ID不存在则插入对象。
	 * 
	 * @param userFavorites
	 * @Author 迷蝶
	 * @return UserFavoritesPo
	 */
	@RequestMapping("save")
	public Integer saveUserFavorites(@RequestBody UserFavoritesPo userFavorites) {
		int rs = 0;
		if(null == userFavorites) {return rs;}
		if(null == userFavorites.getId()) {
			rs = userFavoritesService.addUserFavorites(userFavorites);
		} else {
			rs = userFavoritesService.updateUserFavorites(userFavorites);
		}
		return rs;
	}
	
	/**
	 * 根据ID删除UserFavoritesPo对象，并清除此ID的缓存。<br>
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return UserFavoritesPo
	 */
	@RequestMapping("del/{id:^[0-9]*$}")
	public Integer delUserFavorites(@PathVariable("id") int id) {
		return userFavoritesService.delUserFavorites(id);
	}
	
	/**
	 * 用户取消关注商品
	 * 
	 * @param wareId	商品ID
	 * @param userId		用户ID
	 * @Author 迷蝶
	 * @return Integer
	 */
	@RequestMapping("unfav/{wareId:^[0-9]*$}/{userId:^[0-9]*$}")
	public Integer unFavorite(@PathVariable("wareId") int wareId, @PathVariable("userId") int userId) {
		return userFavoritesService.unFavorite(wareId, userId);
	}
	
	/**
	 * 关注商品
	 * 
	 * @param wareId	商品ID
	 * @param userId		用户ID
	 * @param username		用户名
	 * @Author 迷蝶
	 * @return Integer
	 */
	@RequestMapping("fav/{wareId:^[0-9]*$}/{userId:^[0-9]*$}")
	public Integer favorite(@PathVariable("wareId") int wareId, @PathVariable("userId") int userId, String username) {
		return userFavoritesService.favorite(wareId, userId, username);
	}
	
	/**
	 * 批量取消收藏关注商品
	 * 
	 * @param userId		用户ID，作为条件使用，避免误删
	 * @param ids			关注记录ID集合，多个以逗号分隔
	 * @Author 迷蝶
	 * @return Integer
	 */
	/**
	 * 批量取消收藏关注商品
	 * 
	 * @param userId		用户ID，作为条件使用，避免误删
	 * @param ids			关注记录ID集合，多个以逗号分隔
	 * @Author 迷蝶
	 * @return Integer
	 */
	@RequestMapping("unfavBatch/{userId}")
	public Integer unFavoriteBatch(@PathVariable("userId") int userId, String ids) {
		return userFavoritesService.unFavoriteBatch(userId, ids);
	}
}